import {
	defineStore
} from 'pinia'
import * as API from "../api/api.js"
import {deleteGroupMember, groupMemberList} from "../api/api.js";
export const useGroupStore = defineStore('useGroup', {
	state: () => {
		return {
			groupInfo: null,
      myGroupInfo: null,
			userInfo: null,
			isShowGroupPop: false,
      groupMemberList: [],
      groupType: 0
		}
	},
	actions: {
    resetAll() {
      this.$reset()
      this.groupInfo = null
      this.myGroupInfo = null
			this.userInfo = null
			this.isShowGroupPop = false
      this.groupMemberList = []
      this.groupType = 0
    },
    setGroupType(value) {
      this.groupType = value
    },
		setIsShowGroupPop(value) {
			this.isShowGroupPop = value
		},
    // 团信息根据groupId
    onGetGroupInfoByGroupId(groupId) {
      this.userInfo = uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")) : null
			return new Promise((resolve, reject) => {
        let params = {
          groupId: groupId || this.groupInfo.groupId,
          userId: this.userInfo?.customers?.customerId
        }
				API.getGroupInfoByGroupId(params).then(res => {
					if (res.code == 0) {
            if (res.data) {
              this.groupInfo = {
                ...res.data,
                fullStarffed: res?.data?.limitNumber == res?.data?.memberNumber
              }
            } else {
              this.groupInfo = res.data
            }
						resolve(res)
					} else {
            console.log("团错误通过团ID", res)
          }
				}).catch(err => {
					reject(err)
				})
			})
		},

    // 我的团信息
    onGetGroupInfo() {
			this.userInfo = uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")) : null
			return new Promise((resolve, reject) => {
				API.getGroupInfo(this.userInfo?.customers?.customerId).then(res => {
					if (res.code == 0) {
            if (res.data) {
              this.groupInfo = {
                ...res.data,
                fullStarffed: res?.data?.limitNumber == res?.data?.memberNumber
              }
              this.myGroupInfo = this.groupInfo
            } else {
              this.groupInfo = res.data
              this.myGroupInfo = res.data
            }
						resolve(res)
					} else {
            console.log("团错误", res)
          }
				}).catch(err => {
					reject(err)
				})
			})
		},
    // 团成员信息
    onGetGroupMemberList() {
      this.userInfo = uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")) : null
      return new Promise((resolve, reject) => {
        API.groupMemberList({
          groupId:this.groupInfo.groupId
        }).then(res => {
          if (res.code === 0) {
            this.groupMemberList = res.data
            resolve(res)
          }
        }).catch(err => {
          reject(err)
        })
      })
		},
		onGetGroupRank() {
			state.isMore = false
			return new Promise((resolve, reject) => {
				let params = { 
					groupId: this.groupInfo?.groupId
				}
				API.getGroupRank(params).then(res => {
					if (res.code == 0) {
						resolve(res)
					}
				}).catch(err => {
					reject(err)
				})
			})
		},
		// 弹窗有效期内
		onValidGroupPop() {
			this.isShowGroupPop = true
		},
    // 点赞
    onThumsUp(groupId) {
			return new Promise((resolve, reject) => {
				let params = { 
					groupId,
          userId: this.userInfo?.customers?.customerId
				}
				API.createGroupLike(params).then(res => {
					if (res.code == 0) {
						resolve(res)
					}
				}).catch(err => {
					reject(err)
				})
			})
		},
  }
})

